import 'package:flutter/material.dart';
import 'package:flutter_lifecycle/flutter_lifecycle.dart';
import 'package:flutter_lifecycle_example/home_page/page2_nav2.dart';

import '../log.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BasePageLifecycle(
      onPageShow: (){
        logger.d('onPageShow');
      },
      onInactive: (){
        logger.d('onInactive');
      },
      onForeground: (){
        logger.d('onForeground');
      },
      onBackground: (){
        logger.d('onBackground');
      },
      onPageHide: (){
        logger.d('onPageHide');
      },
      onPageDispose: (){
        logger.d('onPageDispose');
      },

      child: Scaffold(
        appBar: AppBar(
          title: const Text(
            'Home Page',
          ),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              ElevatedButton(
                child: const Text('Open Sub1Page'),
                onPressed: () {
                  Navigator.of(context).pushNamed('sub1');
                },
              ),
              ElevatedButton(
                child: const Text('Open Nav2.0'),
                onPressed: () {
                  Navigator.of(context).pushNamed('nav2');
                  // Navigator.of(context).replace(oldRoute: oldRoute, newRoute: newRoute)
                },
              ),
              ElevatedButton(
                child: const Text('Open dialog.0'),
                onPressed: () {
                  showDialog(
                    context: context,
                    routeSettings: const RouteSettings(name: 'dialog'),
                    builder: (context) {
                      return Center(
                        child: Container(
                          height: 300,
                          width: 300,
                          color: Colors.pink.shade100,
                          child: Nav2Page2(
                            onHidePage2: () {
                              Navigator.of(context, rootNavigator: true)
                                  .maybePop();
                            },
                          ),
                        ),
                      );
                    },
                  );
                },
              ),
              ElevatedButton(
                child: const Text('Open PageView page'),
                onPressed: () {
                  Navigator.of(context).pushNamed('pageView');
                },
              ),
              Expanded(
                child: PageView.builder(itemBuilder: (BuildContext context, int index) {
                  // double widthFactor = 1.0 - (index * 0.2);
                  return Container(
                    width: MediaQuery.of(context).size.width - 50 * index,
                    color: Colors.accents[index],
                    child: Text('index $index'),
                  );
                },
                  itemCount: 4,
                  controller: PageController(initialPage: 0, viewportFraction: 0.8),
                  // padEnds: false,
                  // pageSnapping: true,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}